/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dieci.ui;

import dieci.database.SessionFactoryUtil;
import dieci.managers.clientmanager.ClientManager;
import dieci.reports.JasperReportRunner;
import dieci.system.Client;
import static java.awt.Frame.MAXIMIZED_BOTH;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static java.util.logging.Level.WARNING;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.text.MaskFormatter;
import org.hibernate.Session;

/**
 *
 * @author Pc
 */
public class ReportsUI extends javax.swing.JFrame
{

    private static ReportsUI instance;
    private final Session session;
    private final String userDir = System.getProperty("user.dir");
    private final String reportsPath = userDir + "/src/dieci/reports/";
    private final ClientManager clientManager;
    private Date fechaDesde;
    private Date fechaHasta;
    private String actionCommand;
    
    /**
     * Creates new form WindowRawMaterialProducts
     */
    public ReportsUI()
    {
        clientManager = new ClientManager();
        
        initComponents();
        
        session = SessionFactoryUtil.getInstance().getCurrentSession();
        
        ImageIcon imageIcon = new javax.swing.ImageIcon(getClass().getResource("/dieci/ui/icon_image.jpg"));
        this.setIconImage(imageIcon.getImage());
        this.setExtendedState(MAXIMIZED_BOTH);
        this.setVisible(true);
        
        //set fecha desde y hasta por default
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(0);
        cal.set(2013, 1, 1, 0, 0, 0);
        fechaDesde = cal.getTime();
        fechaHasta = new Date();
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        North = new javax.swing.JPanel();
        East = new javax.swing.JPanel();
        Center = new javax.swing.JPanel();
        jFechaVentaFrame = new javax.swing.JInternalFrame();
        jLabel6 = new javax.swing.JLabel();
        jOkFechaVtaButton = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        MaskFormatter mfFechaDesde = null;
        try {
            mfFechaDesde = new MaskFormatter("##/##/####");
        } catch (ParseException p) {
            p.printStackTrace();
        }
        jFormattedFechaDesde = new javax.swing.JFormattedTextField();
        MaskFormatter mfFechaHasta = null;
        try {
            mfFechaHasta = new MaskFormatter("##/##/####");
        } catch (ParseException p) {
            p.printStackTrace();
        }
        jFormattedFechaHasta = new javax.swing.JFormattedTextField();
        jClientesFrame = new javax.swing.JInternalFrame();
        jLabel1 = new javax.swing.JLabel();
        jClientesComboBox = new javax.swing.JComboBox();
        jOkClienteButton = new javax.swing.JButton();
        jImagenFondo = new javax.swing.JLabel();
        South = new javax.swing.JPanel();
        West = new javax.swing.JPanel();
        jProdPrecioVtaButton = new javax.swing.JButton();
        jProdStockButton = new javax.swing.JButton();
        jVentasClientesButton = new javax.swing.JButton();
        jVentasCobradasButton = new javax.swing.JButton();
        jVentasNoCobradasButton = new javax.swing.JButton();
        jListadoClientesButton = new javax.swing.JButton();
        jVtasNoEntregadasButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jGananciaProdButton = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jComprasProdButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setTitle("Reportes");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        North.setBackground(new java.awt.Color(241, 245, 255));
        North.setPreferredSize(new java.awt.Dimension(1075, 40));

        javax.swing.GroupLayout NorthLayout = new javax.swing.GroupLayout(North);
        North.setLayout(NorthLayout);
        NorthLayout.setHorizontalGroup(
            NorthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1075, Short.MAX_VALUE)
        );
        NorthLayout.setVerticalGroup(
            NorthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 40, Short.MAX_VALUE)
        );

        getContentPane().add(North, java.awt.BorderLayout.NORTH);

        East.setBackground(new java.awt.Color(241, 245, 255));
        East.setPreferredSize(new java.awt.Dimension(30, 541));

        javax.swing.GroupLayout EastLayout = new javax.swing.GroupLayout(East);
        East.setLayout(EastLayout);
        EastLayout.setHorizontalGroup(
            EastLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 30, Short.MAX_VALUE)
        );
        EastLayout.setVerticalGroup(
            EastLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 588, Short.MAX_VALUE)
        );

        getContentPane().add(East, java.awt.BorderLayout.EAST);

        Center.setBackground(new java.awt.Color(241, 245, 255));
        Center.setLayout(null);

        jFechaVentaFrame.setClosable(true);
        jFechaVentaFrame.setTitle("Filtrar por fechas");
        jFechaVentaFrame.setPreferredSize(new java.awt.Dimension(506, 200));
        jFechaVentaFrame.setVisible(false);
        jFechaVentaFrame.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jFechaVentaFrameComponentShown(evt);
            }
        });

        jLabel6.setText("Fecha desde:");

        jOkFechaVtaButton.setText("OK");
        jOkFechaVtaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jOkFechaVtaButtonActionPerformed(evt);
            }
        });

        jLabel7.setText("Fecha hasta:");

        javax.swing.GroupLayout jFechaVentaFrameLayout = new javax.swing.GroupLayout(jFechaVentaFrame.getContentPane());
        jFechaVentaFrame.getContentPane().setLayout(jFechaVentaFrameLayout);
        jFechaVentaFrameLayout.setHorizontalGroup(
            jFechaVentaFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFechaVentaFrameLayout.createSequentialGroup()
                .addGroup(jFechaVentaFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jFechaVentaFrameLayout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel6)
                        .addGap(4, 4, 4)
                        .addComponent(jFormattedFechaDesde, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFormattedFechaHasta, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jFechaVentaFrameLayout.createSequentialGroup()
                        .addGap(180, 180, 180)
                        .addComponent(jOkFechaVtaButton)))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        jFechaVentaFrameLayout.setVerticalGroup(
            jFechaVentaFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFechaVentaFrameLayout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jFechaVentaFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jFormattedFechaDesde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFormattedFechaHasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jOkFechaVtaButton)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        Center.add(jFechaVentaFrame);
        jFechaVentaFrame.setBounds(10, 70, 471, 170);

        jClientesFrame.setClosable(true);
        jClientesFrame.setTitle("Seleccione un cliente");
        jClientesFrame.setVisible(false);

        jLabel1.setText("Cliente:");

        jOkClienteButton.setText("OK");
        jOkClienteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jOkClienteButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jClientesFrameLayout = new javax.swing.GroupLayout(jClientesFrame.getContentPane());
        jClientesFrame.getContentPane().setLayout(jClientesFrameLayout);
        jClientesFrameLayout.setHorizontalGroup(
            jClientesFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jClientesFrameLayout.createSequentialGroup()
                .addGroup(jClientesFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jClientesFrameLayout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jClientesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jClientesFrameLayout.createSequentialGroup()
                        .addGap(112, 112, 112)
                        .addComponent(jOkClienteButton)))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        jClientesFrameLayout.setVerticalGroup(
            jClientesFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jClientesFrameLayout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jClientesFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jClientesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jOkClienteButton)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        Center.add(jClientesFrame);
        jClientesFrame.setBounds(10, 70, 350, 170);

        jImagenFondo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dieci/ui/dieci_image.jpg"))); // NOI18N
        jImagenFondo.setPreferredSize(new java.awt.Dimension(20, 20));
        Center.add(jImagenFondo);
        jImagenFondo.setBounds(200, 6, 1590, 670);

        getContentPane().add(Center, java.awt.BorderLayout.CENTER);

        South.setBackground(new java.awt.Color(241, 245, 255));
        South.setPreferredSize(new java.awt.Dimension(1075, 50));

        javax.swing.GroupLayout SouthLayout = new javax.swing.GroupLayout(South);
        South.setLayout(SouthLayout);
        SouthLayout.setHorizontalGroup(
            SouthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1075, Short.MAX_VALUE)
        );
        SouthLayout.setVerticalGroup(
            SouthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );

        getContentPane().add(South, java.awt.BorderLayout.SOUTH);

        West.setBackground(new java.awt.Color(241, 245, 255));
        West.setPreferredSize(new java.awt.Dimension(388, 588));

        jProdPrecioVtaButton.setText("Productos con precio");
        jProdPrecioVtaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jProdPrecioVtaButtonActionPerformed(evt);
            }
        });

        jProdStockButton.setText("Stock Productos");
        jProdStockButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jProdStockButtonActionPerformed(evt);
            }
        });

        jVentasClientesButton.setText("Ventas por cliente");
        jVentasClientesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jVentasClientesButtonActionPerformed(evt);
            }
        });

        jVentasCobradasButton.setText("Ventas cobradas");
        jVentasCobradasButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jVentasCobradasButtonActionPerformed(evt);
            }
        });

        jVentasNoCobradasButton.setText("Ventas no cobradas");
        jVentasNoCobradasButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jVentasNoCobradasButtonActionPerformed(evt);
            }
        });

        jListadoClientesButton.setText("Listado Clientes");
        jListadoClientesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jListadoClientesButtonActionPerformed(evt);
            }
        });

        jVtasNoEntregadasButton.setText("Ventas no entregadas");
        jVtasNoEntregadasButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jVtasNoEntregadasButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("PRODUCTOS");

        jLabel3.setText("VENTAS");

        jLabel4.setText("CLIENTES");

        jGananciaProdButton.setText("Ganancias Productos");
        jGananciaProdButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jGananciaProdButtonActionPerformed(evt);
            }
        });

        jLabel5.setText("COMPRAS");

        jComprasProdButton.setText("Compras productos");
        jComprasProdButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComprasProdButtonActionPerformed(evt);
            }
        });

        jButton1.setText("Stock Mat. Prima");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout WestLayout = new javax.swing.GroupLayout(West);
        West.setLayout(WestLayout);
        WestLayout.setHorizontalGroup(
            WestLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(WestLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(WestLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(WestLayout.createSequentialGroup()
                        .addComponent(jProdPrecioVtaButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jGananciaProdButton))
                    .addGroup(WestLayout.createSequentialGroup()
                        .addGroup(WestLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jListadoClientesButton)
                            .addComponent(jLabel2)
                            .addComponent(jVentasCobradasButton)
                            .addComponent(jVentasNoCobradasButton)
                            .addComponent(jVtasNoEntregadasButton)
                            .addComponent(jVentasClientesButton)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jComprasProdButton)
                            .addGroup(WestLayout.createSequentialGroup()
                                .addComponent(jProdStockButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        WestLayout.setVerticalGroup(
            WestLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(WestLayout.createSequentialGroup()
                .addComponent(jLabel2)
                .addGap(9, 9, 9)
                .addGroup(WestLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jProdPrecioVtaButton)
                    .addComponent(jGananciaProdButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(WestLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jProdStockButton)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jVentasClientesButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jVentasCobradasButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jVentasNoCobradasButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jVtasNoEntregadasButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addGap(11, 11, 11)
                .addComponent(jListadoClientesButton)
                .addGap(55, 55, 55)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComprasProdButton)
                .addContainerGap(163, Short.MAX_VALUE))
        );

        getContentPane().add(West, java.awt.BorderLayout.LINE_START);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jProdPrecioVtaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jProdPrecioVtaButtonActionPerformed
        String report = reportsPath + "ListadoProductosPrecioVenta.jasper";
        Map parameters = new HashMap();
        
        showReport(report, session, parameters);
    }//GEN-LAST:event_jProdPrecioVtaButtonActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        session.close();
    }//GEN-LAST:event_formWindowClosed

    private void jProdStockButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jProdStockButtonActionPerformed
        String report = reportsPath + "ListadoProductosStock.jasper";
        Map parameters = new HashMap();
        
        showReport(report, session, parameters);
    }//GEN-LAST:event_jProdStockButtonActionPerformed

    private void jVentasClientesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jVentasClientesButtonActionPerformed
        fillClientsComboBox(this.jClientesComboBox);
        
        this.jClientesFrame.show();
    }//GEN-LAST:event_jVentasClientesButtonActionPerformed

    private void jListadoClientesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jListadoClientesButtonActionPerformed
        String report = reportsPath + "ListadoClientes.jasper";
        Map parameters = new HashMap();
        
        showReport(report, session, parameters);
    }//GEN-LAST:event_jListadoClientesButtonActionPerformed

    private void jOkClienteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jOkClienteButtonActionPerformed
        String report = reportsPath + "VentasCliente.jasper";
        Map parameters = new HashMap();
        
        try
        {
            Client cliente = (Client) this.jClientesComboBox.getSelectedItem();
            int clientId = cliente.getClientId();
            parameters.put("cliente", clientId);
        
            showReport(report, session, parameters);
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Ocurrio un error listado el reporte");
            logger.log(WARNING,e.getMessage());
        }
        finally 
        {
            this.jClientesFrame.dispose();
        }
        
    }//GEN-LAST:event_jOkClienteButtonActionPerformed

    private void jOkFechaVtaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jOkFechaVtaButtonActionPerformed
        String desde = jFormattedFechaDesde.getText();
        String hasta = jFormattedFechaHasta.getText();
        
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        if (desde.isEmpty()) {
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(0);
            cal.set(2013, 1, 1, 0, 0, 0);
            fechaDesde = cal.getTime();
        }
        else
        {
            try {    
                fechaDesde = df.parse(desde);
            }
            catch (ParseException ex) {
                JOptionPane.showMessageDialog(null, "Error en la fecha desde");
            }
        }
        
        if (hasta.isEmpty()) {
            fechaHasta = new Date();
        }
        else
        {
            try {    
                fechaHasta = df.parse(hasta);
            }
            catch (ParseException ex) {
                JOptionPane.showMessageDialog(null, "Error en la fecha hasta");
            }
        } 
        
        String methodToCall = actionCommand.replaceAll("\\s+","").trim().toLowerCase();
        
        try
        {
            Method method = ReportsUI.class.getDeclaredMethod(methodToCall, Date.class, Date.class);
            method.invoke(instance, fechaDesde,fechaHasta);
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Ocurrio un error abriendo el reporte");
        }
        finally
        {
            jFechaVentaFrame.hide();
        }
        
        
    }//GEN-LAST:event_jOkFechaVtaButtonActionPerformed

    private void jGananciaProdButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jGananciaProdButtonActionPerformed
        String report = reportsPath + "GananciaProductos.jasper";
        Map parameters = new HashMap();
        
        showReport(report, session, parameters);
    }//GEN-LAST:event_jGananciaProdButtonActionPerformed

    private void jVentasCobradasButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jVentasCobradasButtonActionPerformed
        jFechaVentaFrame.show();
        actionCommand = evt.getActionCommand();
    }//GEN-LAST:event_jVentasCobradasButtonActionPerformed

    private void jFechaVentaFrameComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jFechaVentaFrameComponentShown
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        String anioRet = "2013";
        String mesRet = "01";
        String diaRet = "01";
        
        jFormattedFechaDesde.setText(((diaRet.length()>1)?diaRet:"0"+diaRet)+"/"+((mesRet.length()>1)?mesRet:"0"+mesRet)+"/"+anioRet);
        
        anioRet = String.valueOf(calendar.get(Calendar.YEAR));
        mesRet = String.valueOf(calendar.get(Calendar.MONTH)+1);
        diaRet = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
                
        jFormattedFechaHasta.setText(((diaRet.length()>1)?diaRet:"0"+diaRet)+"/"+((mesRet.length()>1)?mesRet:"0"+mesRet)+"/"+anioRet);
        
        
    }//GEN-LAST:event_jFechaVentaFrameComponentShown

    private void jVentasNoCobradasButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jVentasNoCobradasButtonActionPerformed
        jFechaVentaFrame.show();
        actionCommand = evt.getActionCommand();
        
    }//GEN-LAST:event_jVentasNoCobradasButtonActionPerformed

    private void jVtasNoEntregadasButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jVtasNoEntregadasButtonActionPerformed
        jFechaVentaFrame.show();
        actionCommand = evt.getActionCommand();
    }//GEN-LAST:event_jVtasNoEntregadasButtonActionPerformed

    private void jComprasProdButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComprasProdButtonActionPerformed
        jFechaVentaFrame.show();
        actionCommand = evt.getActionCommand();
    }//GEN-LAST:event_jComprasProdButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String report = reportsPath + "ListadoMatPrimaStock.jasper";
        Map parameters = new HashMap();
        
        showReport(report, session, parameters);
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                instance = new ReportsUI();
                instance.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel Center;
    private javax.swing.JPanel East;
    private javax.swing.JPanel North;
    private javax.swing.JPanel South;
    private javax.swing.JPanel West;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jClientesComboBox;
    private javax.swing.JInternalFrame jClientesFrame;
    private javax.swing.JButton jComprasProdButton;
    private javax.swing.JInternalFrame jFechaVentaFrame;
    private javax.swing.JFormattedTextField jFormattedFechaDesde;
    private javax.swing.JFormattedTextField jFormattedFechaHasta;
    private javax.swing.JButton jGananciaProdButton;
    private javax.swing.JLabel jImagenFondo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JButton jListadoClientesButton;
    private javax.swing.JButton jOkClienteButton;
    private javax.swing.JButton jOkFechaVtaButton;
    private javax.swing.JButton jProdPrecioVtaButton;
    private javax.swing.JButton jProdStockButton;
    private javax.swing.JButton jVentasClientesButton;
    private javax.swing.JButton jVentasCobradasButton;
    private javax.swing.JButton jVentasNoCobradasButton;
    private javax.swing.JButton jVtasNoEntregadasButton;
    // End of variables declaration//GEN-END:variables

    private void showReport(String report, Session session, Map parameters) 
    {
        if(!session.isOpen())
        {
            session = SessionFactoryUtil.getInstance().getCurrentSession();
        }
        
        JasperReportRunner jasperReportRunner = new JasperReportRunner(report, session, parameters);
        try 
        {
            jasperReportRunner.run();
        } 
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }    
    }
    
    private static Logger logger = Logger.getLogger("ReportsUI.log");

    private void fillClientsComboBox(JComboBox jComboBox)
    {
        List<Client> clients = clientManager.list();
        for (Client client : clients) 
        {
            boolean add = true;
            for (int i = 0; i < jComboBox.getModel().getSize(); i++) {
                Client cli = (Client) jComboBox.getModel().getElementAt(i);
                if(client.equals(cli))
                {
                    add = false;
                }
            }
            if(add)
            {
                jComboBox.addItem(client);    
            }
        }        
    }
    
    private void ventascobradas(Date desde, Date hasta)
    {
        String report = reportsPath + "VentasCobradas.jasper";
        String subReport = reportsPath + "TotalVentas.jasper";
        Map parameters = new HashMap();
        parameters.put("fecha_desde", desde);
        parameters.put("fecha_hasta", hasta);
        parameters.put("TotalVentas.jasper", subReport);
        
        showReport(report, session, parameters);
    }

    private void ventasnocobradas(Date desde, Date hasta)
    {
        String report = reportsPath + "VentasCobradas.jasper";
        Map parameters = new HashMap();
        parameters.put("fecha_desde", desde);
        parameters.put("fecha_hasta", hasta);
        
        showReport(report, session, parameters);
    }
    
    private void ventasnoentregadas(Date desde, Date hasta)
    {
        String report = reportsPath + "VentasCobradas.jasper";
        Map parameters = new HashMap();
        parameters.put("fecha_desde", desde);
        parameters.put("fecha_hasta", hasta);
        
        showReport(report, session, parameters);
    }
    
    private void comprasproductos(Date desde, Date hasta)
    {
        String report = reportsPath + "VentasCobradas.jasper";
        Map parameters = new HashMap();
        parameters.put("fecha_desde", desde);
        parameters.put("fecha_hasta", hasta);
        
        showReport(report, session, parameters);
    }
}
